class Roll {
  constructor() {
    this.prize = [] // 奖品
  }

  add(prize, weight) {
    this.prize.push({
      prize,
      weight,
    })
  }

  roll() {
    if (this.prize.length == 0) return

    const lotteryDeivce = [] // 摇号机

    this.prize.map((item, index) => {
      // 将奖品加入到摇号机
      for (let i = 0; i < item.weight * 100; i++) {
        lotteryDeivce.push(index)
      }
    })

    // 打乱奖池
    lotteryDeivce.sort(function () {
      return 0.5 - Math.random()
    })

    // 获取摇号机抽中下标
    const lotteryDeivceIndex = this.getRandom(0, lotteryDeivce.length - 1)

    // 换算出奖品下标
    const prizeIndex = lotteryDeivce[lotteryDeivceIndex]

    // 获取中奖物品信息
    const prize = this.prize[prizeIndex]

    console.log('this.prize', this.prize)

    return prize.prize
  }

  // 获取2个值之间的随机数
  getRandom(min, max) {
    var Range = max - min
    var Rand = Math.random()
    return min + Math.round(Rand * Range)
  }
}

// let roller = new Roll()
// roller.add('中奖', 90)
// roller.add('不中奖', 1)
// while (1) {
//   console.log(roller.roll())
// }

module.exports = Roll
